﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tomato.Cryptography;
using System.Security.Cryptography;
using Tomato.Text;
using System.IO;

namespace Tomato.Helpers
{
    public static class CryptographyHelper
    {
        public static string EnryptWithDES(string s, string key)
        {
            SymmetricEncryption se = new SymmetricEncryption(new DESCryptoServiceProvider());
            se.Key = new Key(key);
            return se.EncryptAsBase64(s);
        }

        public static void EncryptWithDES(string sourceFileName, string targetFileName, string key)
        {
            using (FileStream sourceStream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read))
            {
                using (FileStream targetStream = new FileStream(targetFileName, FileMode.Create, FileAccess.Write))
                {
                    SymmetricEncryption se = new SymmetricEncryption(new DESCryptoServiceProvider());
                    se.Key = new Key(key);
                    se.Encrypt(sourceStream, targetStream);
                }
            }
        }

        public static string DecryptWithDES(string s, string key)
        {
            SymmetricEncryption se = new SymmetricEncryption(new DESCryptoServiceProvider());
            se.Key = new Key(key);
            return se.DecryptFromBase64(s);
        }

        public static void DecryptWithDES(string sourceFileName, string targetFileName, string key)
        {
            using (FileStream sourceStream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read))
            {
                using (FileStream targetStream = new FileStream(targetFileName, FileMode.Create, FileAccess.Write))
                {
                    SymmetricEncryption se = new SymmetricEncryption(new DESCryptoServiceProvider());
                    se.Key = new Key(key);
                    se.Decrypt(sourceStream, targetStream);
                }
            }
        }



        public static string EnryptWithXor(string s, string key)
        {
            SymmetricEncryption se = new SymmetricEncryption(new XorCryptoServiceProvider());
            se.Key = new Key(key);
            return se.EncryptAsBase64(s);
        }

        public static void EncryptWithXor(string sourceFileName, string targetFileName, string key)
        {
            using (FileStream sourceStream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read))
            {
                using (FileStream targetStream = new FileStream(targetFileName, FileMode.Create, FileAccess.Write))
                {
                    SymmetricEncryption se = new SymmetricEncryption(new XorCryptoServiceProvider());
                    se.Key = new Key(key);
                    se.Encrypt(sourceStream, targetStream);
                }
            }
        }

        public static string DecryptWithXor(string s, string key)
        {
            SymmetricEncryption se = new SymmetricEncryption(new XorCryptoServiceProvider());
            se.Key = new Key(key);
            return se.DecryptFromBase64(s);
        }


        public static string ComputeMD5(string s)
        {
            return TextConversion.ToHexString(new HashConversion(MD5.Create()).ComputeHash(s));
        }
    }
}
